package com.lnyt.util.factroy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lnyt.dao.t12306.TicketStore;
import com.lnyt.pojo.ticket.TicketTask;
import com.lnyt.pojo.ticket.TicketUser;
import com.lnyt.pojo.ticket.TrainNumber;
import com.lnyt.util.LogUtil;
import com.lnyt.util.TicketUtil;
import org.springframework.stereotype.Component;

import com.lnyt.pojo.ticket.TrainStation;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;


@Component
public class TicketFactroy {

    // 生成使用用户对象
    public static TicketUser creatTicketUser() {
        TicketUser ticketUser = new TicketUser();
        ticketUser.setTicketStatus(TicketStore.NOLOGIN);
        ticketUser.setTicketTaskMap(new HashMap<>());

        return ticketUser;
    }

    // 解析车站
    public static TrainStation createTrainStation(String data) {
        TrainStation trainStation = new TrainStation();
        String[] items = data.split("\\|");

        if (items.length == 6) {
            trainStation.setPinYin1(items[0]);
            trainStation.setPinYin2(items[3]);
            trainStation.setPinYin3(items[4]);
            trainStation.setNameCN(items[1]);
            trainStation.setSymbol(items[2]);
            trainStation.setStationID(Integer.valueOf(items[5]));
            trainStation.setCount(0);
        }

        return trainStation;
    }

    // 解析查询车次
    public static TrainNumber createTrainNumber(String data) {
        TrainNumber result = new TrainNumber();
        String[] items = data.split("\\|");

        result.setSecretStr(items[0]);
        result.setReservation(items[1]);
        result.setCanOrder(items[11]);

        result.setTrainCdoe(items[2]);
        result.setTrainNumber(items[3]);
        result.setTrainLocation(items[15]);

        result.setOriginating(items[4]);
        result.setTerminus(items[5]);
        result.setFromStation(items[6]);
        result.setToStation(items[7]);

        result.setFromTime(items[8]);
        result.setToTime(items[9]);

        result.setBedPGRW(items[21]);
        result.setBedPRW(items[23]);
        result.setBedPYW(items[28]);
        result.setBedDDW(items[33]);

        result.setStand(items[26]);
        result.setSeatPRZ(items[24]);
        result.setSeatPYZ(items[29]);

        result.setSeatDEDZ(items[30]);
        result.setSeatDYDZ(items[31]);
        result.setSeatDSWZ(items[32]);
        result.setSeatDTDZ(items[25]);

        return result;
    }

    // 解析未完成订单信息
    public static TicketTask parseOrderDBList(JSONObject data) {
        TicketTask ticketTask = new TicketTask();
        JSONObject ticketData = new JSONObject();

        try {
            if (data.containsKey("tickets")) {
                JSONArray ticketList = data.getJSONArray("tickets");
                ticketData = ticketList.getJSONObject(0);
            }

            if (ticketData.containsKey("stationTrainDTO")) {
                JSONObject trainData = ticketData.getJSONObject("stationTrainDTO");
                ticketTask.setTrainNumber(trainData.getString("station_train_code"));

                ticketTask.setFromStation(trainData.getString("from_station_telecode"));
                ticketTask.setFromStationName(trainData.getString("from_station_name"));

                ticketTask.setToStation(trainData.getString("to_station_telecode"));
                ticketTask.setToStationName(trainData.getString("to_station_name"));
            }

            if (ticketData.containsKey("passengerDTO")) {
                JSONObject passengerData = ticketData.getJSONObject("passengerDTO");

                ticketTask.setPassengerName(passengerData.getString("passenger_name"));
                ticketTask.setPassengerIdType(passengerData.getString("passenger_id_type_code"));
                ticketTask.setPassengerType(passengerData.getString("passenger_id_type_name"));
                ticketTask.setPassengerId(passengerData.getString("passenger_id_no"));
            }

            if (ticketData.containsKey("train_date")) {
                Date trainDate = LogUtil.parseDateStr(ticketData.getString("train_date"), "yyyy-MM-dd hh:mm:ss");
                ticketTask.setTrainDate(LogUtil.formatDate(trainDate, "yyyy-MM-dd"));
            }

            if (ticketData.containsKey("sequence_no")) {
                ticketTask.setOrderId(ticketData.getString("sequence_no"));
            }

            if (ticketData.containsKey("pay_limit_time")) {
                ticketTask.setLostTime(ticketData.getString("pay_limit_time"));
            }

            if (ticketData.containsKey("seat_type_code")) {
                ticketTask.setTicketType(ticketData.getString("seat_type_code"));
            }

            ticketTask.setTrainId(TicketUtil.calcTrainMd5(ticketTask));

        } catch (Exception exc) {
            exc.printStackTrace();
        }


        return ticketTask;
    }
}
